package hz.mobileproject.pda.presenter;


import android.content.Context;

import com.google.gson.Gson;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.ui.activity.work.ImpBuWeiActivity;
import hz.mobileproject.pda.utils.Base64;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by zmybi on 2017/8/28.
 */

public class PImpBuWei extends XPresent<ImpBuWeiActivity> {

    private final String token;
    private  Request_data data;
    private  Gson gson;
    private String mRequestData;
    private RequestBody body;
    private String body_data;



    public PImpBuWei() {
        gson = new Gson();
        data = new Request_data();
        this.token = getV().getApplicationContext().getSharedPreferences("app_login", Context.MODE_PRIVATE).getString("token","");
    }

    public void setData(String request_data, int type) {
        this.mRequestData = request_data;
        data.setData(Base64.getBase64(request_data));
        body_data = gson.toJson(data);   //将请求类转化为请求String
        body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),body_data);
        getDataByBody(body,type);
    }

    private void getDataByBody(RequestBody body, int type) {
        switch (type) {
            case 1:{   //民警基本信息获取
                Api.getGankService().getloadPoliceInfo(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),1);
                            }
                        });

            }
            break;
            case 2: {   //开始
                Api.getGankService().startImp(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),2);
                            }
                        });
            }
            break;
            case 3: {   //查询重点部位检查列表
                Api.getGankService().searchImpList(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),3);
                            }
                        });
            }
            break;
            case 4: {   //清点

                Api.getGankService().impCalling(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().Error(error,mRequestData,4);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),4);
                            }
                        });
            }
            break;
            case 5: {   //没有提交的清点 数据库中再次提交
                Api.getGankService().impCalling(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().Error(error,mRequestData,4);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),5);
                            }
                        });
            }
            break;
            case 6: {
                Api.getGankService().impEnd(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().Error(error,mRequestData,6);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),6);
                            }
                        });
            }
            break;
            case 7 : {
                Api.getGankService().getBackDialogContent(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                getV().setView(Base64.getFromBase64(response_data.getData()),7);
                            }
                        });
            }
            break;
        }
    }
}
